<template>
  <div class="page-container add-project-contaienr">
    <div class="form-container">
      <!-- 项目名称 -->
      <div class="form-item">
        <div class="title">项目名称：</div>
        <el-input v-model="projectDetail.name"></el-input>
      </div>
      <!-- 项目描述 -->

      <div class="form-item">
        <div class="title">项目描述：(每一项描述以英文逗号为分割)</div>
        <el-input v-model="projectDetail.description"></el-input>
      </div>
      <!-- 项目链接 -->

      <div class="form-item">
        <div class="title">项目链接：</div>
        <el-input v-model="projectDetail.url"></el-input>
      </div>
      <!-- Github地址 -->

      <div class="form-item">
        <div class="title">Github地址：</div>
        <el-input v-model="projectDetail.github"></el-input>
      </div>
      <!-- 预览图 -->
      <div class="form-item">
        <div class="title">预览图：</div>
        <Upload :image-url="projectDetail.thumb" :change-image-url="handleImage" />
      </div>

      <!-- 排序等级 -->

      <div class="form-item select">
        <div class="title">排序等级:</div>
        <el-select
          v-model="projectDetail.order"
          class="m-2"
          placeholder="排序等级"
          size="large"
        >
          <el-option v-for="item in initOrder" :key="item" :label="item" :value="item" />
        </el-select>
      </div>
      <!-- 按钮 -->
      <div class="form-item">
        <el-button
          type="primary"
          @click="handlAddProject"
          :loading="isLoading"
          size="large"
          >添加项目</el-button
        >
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { IProjectObj, addProject } from "@/api/project";
import Upload from "@/components/Upload.vue";
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

// 修改详情
const projectDetail = reactive<IProjectObj>({
  description: "",
  name: "",
  url: "",
  github: "",
  thumb: "",
  order: 1,
});

// 路由
const router = useRouter();

// 确定是否加载
const isLoading = ref<boolean>(false);

//修改预览图
function handleImage(url: string) {
  projectDetail.thumb = url;
}

// 添加项目
async function handlAddProject() {
  const submitObj = { ...projectDetail };
  submitObj.description = (submitObj.description as string).split(",");
  isLoading.value = true;
  const res = await addProject(submitObj);
  if (res.code === 0) {
    isLoading.value = false;
    router.push("/project");
    ElMessage({
      type: "success",
      message: "添加项目成功",
      durtaion: 1500,
    });
  } else {
    ElMessage({
      type: "error",
      message: res.msg,
    });
  }
}

// 初始化order的内容
const initOrder = [1, 2, 3, 4, 5];
</script>

<style lang="less" scoped>
.form-container {
  margin-top: -25px;
  .form-item {
    .title {
      margin: 20px 0;
      font-size: 17px;
      color: #666;
      font-weight: bold;
    }
    &.select {
      margin: 20px 0;
    }
  }
}
</style>
